package com.demo8.basic.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.convert.Convert;
import com.demo8.system.domain.dto.SysUserMenuDTO;
import com.demo8.system.domain.dto.SysUserRoleDTO;
import com.demo8.system.mapper.SysUserMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
public class StpInterfaceImplConfig implements StpInterface {
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<SysUserMenuDTO> userPermssionList = sysUserMapper.getUserPermssionList(Convert.toLong(loginId));
        if (CollectionUtils.isNotEmpty(userPermssionList)) {
            return userPermssionList.stream().map(SysUserMenuDTO::getMenuPerms).collect(Collectors.toList());
        }

        return Collections.emptyList();
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<SysUserRoleDTO> userRoleList = sysUserMapper.getUserRoleList(Convert.toLong(loginId));
        if (CollectionUtils.isNotEmpty(userRoleList)) {
            return userRoleList.stream().map(SysUserRoleDTO::getRoleName).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
